package com.flashcash.solu.view.me.presenter;

import android.text.TextUtils;

import com.flashcash.solu.app.base.presenter.BaseActivityPresenterImpl;
import com.flashcash.solu.common.FieldParams;
import com.flashcash.solu.utils.ACache;
import com.flashcash.solu.view.me.AboutView;
import com.flashcash.kotlin.data.DisplayBean;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @作者:My
 * @创建日期: 2017/6/21 17:28
 * @描述:${TODO}
 * @更新者:${Author}$
 * @更新时间:${Date}$
 * @更新描述:${TODO}
 */

public class AboutPreImp extends BaseActivityPresenterImpl implements AboutActPresenter {

    @Override
    public void requestDisplay() {
        mUserApi.display()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.bindToLifecycle())
                .subscribe(new Subscriber<DisplayBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        Object object = ACache.get(mView.getBaseActivity()).getAsObject
                                (FieldParams.ACacheKeys.ACACHE_DISPLAY_BEAN);
                        if (object != null && object instanceof DisplayBean) {
                            DisplayBean displayBean = (DisplayBean) object;
                            if (TextUtils.isEmpty(displayBean.getDescription())) {
                                ((AboutView) mView).responseDisplayEmpty();
                            } else {
                                ((AboutView) mView).responseDisplay(displayBean);
                            }
                        } else {
                            ((AboutView) mView).responseDisplayEmpty();
                        }
                    }

                    @Override
                    public void onNext(DisplayBean displayBean) {
                        dismissLoading();
                        ACache.get(mView.getBaseActivity()).put(FieldParams.ACacheKeys
                                .ACACHE_DISPLAY_BEAN, displayBean);
                        if (displayBean == null || TextUtils.isEmpty(displayBean.getDescription())) {
                            ((AboutView) mView).responseDisplayEmpty();
                        } else {
                            ((AboutView) mView).responseDisplay(displayBean);
                        }
                    }
                });
    }

}
